package com.apps.sdses.flink141.source;

import com.apps.sdses.flink141.entity.Vehicle;
import org.apache.flink.streaming.api.functions.source.RichSourceFunction;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * @author jiwei
 * @description
 * @date 2023/5/31 12:25
 */
public class VehicleSource extends RichSourceFunction<Vehicle> {

    Random random = new Random();
    boolean runFlag = true;
    List<String> vehicleType = Arrays.asList("天然气", "汽油", "液化石油气", "柴油", "污水");
    List<String> vehicleBrand = Arrays.asList("丰田", "本田", "长城", "福特");

    @Override
    public void run(SourceContext<Vehicle> ctx) throws Exception {
        for (int i = 0; i < 20; i++) {
//            Vehicle vehicle = new Vehicle();
//            vehicle.setVehicleId("车辆编号1000" + i);
//            vehicle.setVehicleType(vehicleType.get(random.nextInt(vehicleType.size())));
//            vehicle.setVehicleBrand(vehicleBrand.get(random.nextInt(vehicleBrand.size())));
//            vehicle.setLicensePlateNumber(generateLicensePlate());
//            ctx.collect(vehicle);
//            Thread.sleep(2000);
        }
    }

    @Override
    public void cancel() {

    }

    // 生成车牌号码
    private String generateLicensePlate() {
        StringBuilder licensePlate = new StringBuilder();
        // 生成省份简称
        String[] provinces = {"京", "津", "冀", "晋", "蒙", "辽", "吉", "黑", "沪", "苏", "浙", "皖", "闽", "赣", "鲁", "豫", "鄂", "湘", "粤", "桂", "琼", "渝", "川", "贵", "云", "藏", "陕", "甘", "青", "宁", "新", "台", "港", "澳", "使"};
        licensePlate.append(provinces[random.nextInt(provinces.length)]);
        // 生成字母和数字部分
        String[] chars = {"A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
        for (int i = 0; i < 5; i++) {
            licensePlate.append(chars[random.nextInt(chars.length)]);
        }
        return licensePlate.toString();
    }
}
